<?

/***************************************************************************
 Jupiter Content System @ Highstrike.net
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Filter Access Rights
//-----------------------------------------------------------------

if(isset($is_guest) || isset($is_user))
{ header("location: $PHP_SELF?i=2"); exit; }

//-----------------------------------------------------------------
//  Begin Switch
//-----------------------------------------------------------------

switch($a)
{
	//-----------------------------------------------------------------
	//  Add host case
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Start Checks
		//-----------------------------------------------------------------

		if(!$host){ header("location: $PHP_SELF?n=modules/status&i=135"); exit; }
		if(!$port){ header("location: $PHP_SELF?n=modules/status&i=136"); exit; }
		$checker = $db->getLine("SELECT * FROM status WHERE host = '$host' AND port = '$port'");
		if($checker != FALSE){ header("location: $PHP_SELF?n=modules/status&i=137"); exit; }

		//-----------------------------------------------------------------
		//  Add in table
		//-----------------------------------------------------------------

		$db->insertRow("status",array('id'=>'','host'=>''.$host.'','port'=>''.$port.''));
		
		//-----------------------------------------------------------------
		//  Add Log
		//-----------------------------------------------------------------
		
		$db->insertRow("log",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','log' => ''.$db->escape($language['Logs desc3']).' <b>status</b> the host/ip -> \''.$db->escape($host).'"','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/status&i=148");

	break;

	//-----------------------------------------------------------------
	//  Delete host case
	//-----------------------------------------------------------------

	case 2:

		//-----------------------------------------------------------------
		//  Get Host
		//-----------------------------------------------------------------		
	
		$host = $db->getLine("SELECT id, host, port FROM status",$d);
		
		//-----------------------------------------------------------------
		//  Delete from table
		//-----------------------------------------------------------------

		$db->deleteRow("status","id = '$d'");
		
		//-----------------------------------------------------------------
		//  Add Log
		//-----------------------------------------------------------------
		
		$db->insertRow("log",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','log' => ''.$db->escape($language['Logs desc']).' <b>status</b> the host/ip -> \''.$db->escape($host['host']).'"','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/status&i=149");

	break;

	//-----------------------------------------------------------------
	//  View Help case
	//-----------------------------------------------------------------

	case 3:
	?>
			
		<table class="main" cellspacing="1" cellpadding="4">
		<tr class='con1'><td><a href='<?= $PHP_SELF ?>?n=modules/admin'><?= $language['Admin name'] ?></a> &raquo; <a href='<?= $PHP_SELF ?>?n=modules/status'><?= $language['Host title'] ?></a> &raquo; <?= $language['Host title2'] ?></td></tr>
		</table>
		<table><tr><td></td></tr></table>
			
		<table class="main" cellspacing="1" cellpadding="4">
		<tr class='head'>
		<td colspan='3'> <?= $language['Guide desc'] ?></td>
		</tr>
		<tr class='bottom'>
		<td width="60%" colspan="3"><?= $language['Guide desc2'] ?></td>
		</tr>
		<tr class='con1'>
		<td colspan="3"><?= $language['Guide desc3'] ?> <a href="http://www.iana.org/assignments/port-numbers" target="_blank"><?= $language['Guide desc4'] ?></a></td>
		</tr>
		<tr class='bottom'>
		<td width="1%"><div align="center">#</div></td>
		<td><?= $language['Guide desc5'] ?></td>
		<td><?= $language['Guide desc6'] ?></td>
		</tr>
		<tr class='con1'>
		<td><div align="center">1</div></td>
		<td>80</td>
		<td><?= $language['Port desc5'] ?></td>
		</tr>
		<tr class='con1'>
		<td><div align="center">2</div></td>
		<td>21</td>
		<td><?= $language['Port desc'] ?></td>
		</tr>
		<tr class='con1'>
		<td><div align="center">3</div></td>
		<td>22</td>
		<td><?= $language['Port desc2'] ?></td>
		</tr>
		<tr class='con1'>
		<td><div align="center">4</div></td>
		<td>25</td>
		<td><?= $language['Port desc4'] ?></td>
		</tr>
		<tr class='con1'>
		<td><div align="center">5</div></td>
		<td>110</td>
		<td><?= $language['Port desc6'] ?></td>
		</tr>
		<tr class='con1'>
		<td><div align="center">6</div></td>
		<td>143</td>
		<td><?= $language['Port desc7'] ?></td>
		</tr>
		<tr class='con1'>
		<td><div align="center">7</div></td>
		<td>443</td>
		<td><?= $language['Port desc8'] ?></td>
		</tr>
		<tr class='con1'>
		<td><div align="center">8</div></td>
		<td>2082</td>
		<td><?= $language['Port desc9'] ?></td>
		</tr>
		<tr class='con1'>
		<td><div align="center">9</div></td>
		<td>2095</td>
		<td><?= $language['Port desc10'] ?></td>
		</tr>
		<tr class='con1'>
		<td><div align="center">10</div></td>
		<td>3306</td>
		<td><?= $language['Port desc11'] ?></td>
		</tr>
		<tr class='con1'>
		<td><div align="center">11</div></td>
		<td>27015</td>
		<td><?= $language['Port desc12'] ?></td>
		</tr>
		</table>
		<? 
	
	break;

	//-----------------------------------------------------------------
	//  Show Server List as Default case
	//-----------------------------------------------------------------

	default:

		//-----------------------------------------------------------------
		//  Navigation Stuff
		//-----------------------------------------------------------------

		$rows_per_page = "20";
		$numrows = $db->getInfo_countRows("status");

		$lastpage = ceil($numrows/$rows_per_page);

		$p = (int)$p;

		if ($p < 1) $p = 1;
		elseif ($p > $lastpage) $p = $lastpage;

		$limit = 'LIMIT ' .($p - 1) * $rows_per_page .',' .$rows_per_page;

		//-----------------------------------------------------------------
		//  Sorting Stuff
		//-----------------------------------------------------------------

		if(!isset($s)) $s = 'id'; // Default value
		if(!isset($t)) $t = 'ASC'; // Default value

		$sname1 = "id";
		$sname2 = "host";
		$sname3 = "port";
		$sname4 = "online";

		if($t == "ASC"){ $t = "DESC"; $t2 = "ASC"; }
		else{ $t = "ASC"; $t2 = "DESC"; }

		if($t == "ASC"){ $t3 = "<img src='templates/$template/images/misc/up.gif' border='0'>";}
		else{ $t3 = "<img src='templates/$template/images/misc/down.gif' border='0'>";}

		if($s == 1) $s = $sname1;
		if($s == 2) $s = $sname2;
		if($s == 3) $s = $sname3;
		if($s == 4) $s = $sname4;

		//-----------------------------------------------------------------
		//  Check and Write if he is online
		//-----------------------------------------------------------------

		$checker = $db->getTable("SELECT id, host, port, online FROM status");

		if($checker != FALSE)
		{
			foreach($checker as $check)
			{
				$server = @fsockopen($check['host'], $check['port'], $errno, $errstr, 1);

				if($server)
					if($check['online'] == 1){}
					else $db->updateRow("status",array('online' => '1'),"id = {$check['id']}");
				else
					if($check['online'] == 0){}
					else $db->updateRow("status",array('online' => '0'),"id = {$check['id']}");
			}
		}

		//-----------------------------------------------------------------
		//  Load the status table
		//-----------------------------------------------------------------

		$status = $db->getTable("SELECT id, host, port, online FROM status ORDER BY '$s' $t2 $limit");

		//-----------------------------------------------------------------
		//  Make a foreach to separate the results
		//-----------------------------------------------------------------

		if($status != FALSE)
		{
			foreach($status as $idx=>$stat)
			{
				//-----------------------------------------------------------------
				//  Count for ID + trick
				//-----------------------------------------------------------------

				if($s == $sname1 && $t == "ASC")
					$idx_count = str_replace("-", "", $idx + (($p - 1) * $rows_per_page + 1) - $numrows - 1);
				else
					$idx_count = $idx + (($p - 1) * $rows_per_page + 1);

				//-----------------------------------------------------------------
				//  Check server if he is online/offline
				//-----------------------------------------------------------------

				if($stat['online'] == "1")
				{
					$check = "<font color='green'>{$language['Host desc']}</font>";
					$link = "<a href='http://{$stat['host']}:{$stat['port']}' target='_blank'>{$stat['host']}</a>";
				}
				else
				{
					$check = "<font color='red'>{$language['Host desc2']}</font>";
					$link = $stat['host'];
				}

				//-----------------------------------------------------------------
				//  Identify the port
				//-----------------------------------------------------------------

				if($stat['port'] == "21") $port = "{$language['Port desc']}";
				elseif($stat['port'] == "22") $port = "{$language['Port desc2']}";
				elseif($stat['port'] == "23") $port = "{$language['Port desc3']}";
				elseif($stat['port'] == "25") $port = "{$language['Port desc4']}";
				elseif($stat['port'] == "80") $port = "{$language['Port desc5']}";
				elseif($stat['port'] == "110") $port = "{$language['Port desc6']}";
				elseif($stat['port'] == "143") $port = "{$language['Port desc7']}";
				elseif($stat['port'] == "443") $port = "{$language['Port desc8']}";
				elseif($stat['port'] == "2082") $port = "{$language['Port desc9']}";
				elseif($stat['port'] == "2095") $port = "{$language['Port desc10']}";
				elseif($stat['port'] == "3306") $port = "{$language['Port desc11']}";
				elseif($stat['port'] == "27015") $port = "{$language['Port desc12']}";
				else $port = $stat['port'];

				//-----------------------------------------------------------------
				//  Make Layout
				//-----------------------------------------------------------------

				$layout .= "<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."'><td align='center'>$idx_count</td>";
				$layout .= "<td>$link</td>";
				$layout .= "<td>$port</td>";
				$layout .= "<td>$check</td>";

				if(isset($is_webmaster) || isset($is_administrator))
					$layout .= "<td><a href='?n=modules/status&a=2&d={$stat['id']}&s=$s&t=$t2&p=$p'><img src='templates/$template/images/buttons/delete.gif' border='0' alt='{$language['Ind desc']}'></a></td>";

				$layout .= "</tr>";

				//-----------------------------------------------------------------
				//  Start from 1
				//-----------------------------------------------------------------

				$idx++;
			}
		}
		else
		{
			//-----------------------------------------------------------------
			//  Reset to 0
			//-----------------------------------------------------------------

			$idx = 0;

			//-----------------------------------------------------------------
			//  Make Layout
			//-----------------------------------------------------------------

			$layout .= "<tr class='con1'><td colspan='5'>{$language['Host desc3']}</td></tr>";
		}

		//-----------------------------------------------------------------
		//  Navigation Code
		//-----------------------------------------------------------------

		if ($p == 1) $nav_prev = "<tr><td class='con1' align='right'>&nbsp;</td>";
		else
		{
			$prevpage = $p-1;
			$nav_prev = "<tr><td class='con1' align='left'><a href='$PHP_SELF?n=modules/status&s=$s&t=$t2&p=$prevpage'>&laquo; {$language['Host desc4']}</a></td>";
		}

		$nav_nr = '[ '.paginate($lastpage, $p, $PHP_SELF.'?n=modules/status&s='.$s.'&t='.$t2).' ]';

		if($idx == $numrows) $nav_nr = NULL;

		if ($p == $lastpage)
			$nav_next = "<td class='con1' align='right'>&nbsp;</td></tr>";
		else
		{
			$nextcode = $numrows - $idx * $p;
			if($nextcode > $idx) $how_next = $idx;
			else $how_next = $nextcode;

			$nextpage = $p+1;

			$nav_next = "<td class='con1' align='right'><a href='$PHP_SELF?n=modules/status&s=$s&t=$t2&p=$nextpage'>{$language['Host desc5']}$how_next &raquo;</a></td></tr>";
		}

		if($idx == $numrows)
		{
			$nav_head = "class='con1'";
			$nav = NULL;
		}
		else
		{
			$nav_head = "class='head'";
			$nav = ''.$nav_prev.''.$nav_next.'';
		}

		//-----------------------------------------------------------------
		//  Start Layout + Navigation
		//-----------------------------------------------------------------

			if(isset($is_administrator) || isset($is_webmaster)) {?>
			
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='con1'><td><a href='<?= $PHP_SELF ?>?n=modules/admin'><?= $language['Admin name'] ?></a> &raquo; <?= $language['Host title'] ?></td></tr>
			</table>
			<table><tr><td></td></tr></table>			
			
			<form name="status" method="post" action="?n=modules/status">
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='head'>
			<td colspan='3'><?= $language['Host desc6'] ?> | <? echo "<a href='$PHP_SELF?n=modules/status&a=3'>{$language['Host desc7']}</a>"; ?></td>
			</tr>
			<tr class='bottom'>
			<td width="60%"><?= $language['Host desc8'] ?></td>
			<td width="20%"><?= $language['Host desc9'] ?></td>
			<td width="20%"><?= $language['Host desc10'] ?></td>
			</tr>
			<tr class='con1'>
			<td><input name="host" type="text" class="box" style="width:100%" id="host" maxlength="100"></td>
			<td><input name="port" type="text" class="box" style="width:100%" id="port" value="80" maxlength="10"></td>
			<td><input type="submit" name="Submit" class="box" style="width:100%" value="<? echo $language['Host desc11']; ?>"></td>
			</tr>
			</table>
			<input type="hidden" name="a" value="1">
			</form>
			<table><tr><td></td></tr></table>
			
			<? } ?>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='head'><td colspan=<? if(isset($is_webmaster) || isset($is_administrator)) echo "5"; else echo "4"; ?>><?= $language['Host desc12'] ?></td></tr>
			<tr class='bottom'>
			<td align="center"><a href="<?= $PHP_SELF ?>?n=modules/status&s=1&t=<?= $t ?>&p=<?= $p ?>"><? if($s == $sname1) echo $t3; else echo "#" ?></a></td>
			<td width="60%"><a href="<?= $PHP_SELF ?>?n=modules/status&s=2&t=<?= $t ?>&p=<?= $p ?>"><?= $language['Host desc13'] ?> <? if($s == $sname2) echo $t3 ?></a></td>
			<td width="30%"><a href="<?= $PHP_SELF ?>?n=modules/status&s=3&t=<?= $t ?>&p=<?= $p ?>"><?= $language['Host desc14'] ?> <? if($s == $sname3) echo $t3 ?></a></td>
			<td width="20%"><a href="<?= $PHP_SELF ?>?n=modules/status&s=4&t=<?= $t ?>&p=<?= $p ?>"><?= $language['Host desc15'] ?> <? if($s == $sname4) echo $t3 ?></a></td>
			<? if(isset($is_administrator) || isset($is_webmaster)) {?><td width="1%">&nbsp;</td> <? } ?>
			</tr>
			<? echo $layout; ?>
			</table>
			<table><tr><td></td></tr></table>
			
			<table class="main" cellspacing="1" cellpadding="4" width="100%">
			<col width="50%"><col width="50%">
			<tr <?= $nav_head ?>>
			<td colspan="2">
			<table border="0" cellspacing="0" cellpadding="0" width="100%">
			<col width="50%"><col width="50%">
			<tr class="empty">
			<td align="left"><?= $language['Host desc16'] ?> <b><?= $idx ?></b> <?= $language['Host desc17'] ?> <b><?= $numrows ?></b></td>
			<td align="right"><?= $nav_nr ?></td>
			</tr>
			</table>
			</td>
			</tr>
			<?= $nav ?>
			</table>
		<?

	break;
}

?>